package rjgc.mall.service.impl;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import rjgc.mall.entity.*;
import rjgc.mall.entity.vo.Order2;
import rjgc.mall.entity.vo.ShoppingCarList;
import rjgc.mall.entity.vo.ShoppingCart2;
import rjgc.mall.entity.vo.UserHistoryOrders;
import rjgc.mall.mapper.*;
import rjgc.mall.service.CollectionService;
import rjgc.mall.service.ShoppingCartService;
import rjgc.mall.service.UserService;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private TradeActionMapper tradeActionMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private UserService userService;
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private CollectionMapper collectionMapper;
    //添加至购物车
    @Override
    public int AddToShoppingCart(Integer userid,Goods good) {
        ShoppingCart shoppingcart=new ShoppingCart();
        shoppingcart.setGoodid(good.getId());
        shoppingcart.setGoodname(good.getName());
        shoppingcart.setGoodprice(good.getPrice());
        shoppingcart.setUserid(userid);
        shoppingcart.setSelected(0);
        shoppingcart.setNum(1);
/*        shoppingcart.setGoodimgUrl(good.getImgurl());
        shoppingcart.setGoodDescription(good.getDescription());
        shoppingcart.setGoodType(good.getType());
        shoppingcart.setGoodStatus(good.getStatus());
        shoppingcart.setGoodSupplyId(good.getSupplyId());
        shoppingcart.setGoodSupplyTime(good.getSupplyTime());
        shoppingcart.setGoodStock(good.getStock());
        shoppingcart.setGoodimageId(good.getImage_id());
        shoppingcart.setGoodSeasonal(good.getSeasonal());*/

        if (shoppingCartMapper.userAndgoodExistOrNot(good.getId(),userid).isEmpty()){
            return shoppingCartMapper.insertToDatabase(shoppingcart);
        }else{
            //System.out.println(shoppingCartMapper.userAndgoodExistOrNot(good.getId(),userid));
            return 2;//表示已经被收藏过
        }

    }
    //根据购物车id删除购物车物品
    @Override
    public int RemoveFromShoppingCart(Integer id) {
        return shoppingCartMapper.removeById(id);
    }
    //根据被选中状态删除商品
    @Override
    public int RemoveFromShoppingCartByStatus(Integer userid) {
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.selectWhereSelectedEqualsOne(userid);
        for (int i=0;i<shoppingCartList.size();i++){
            int id=shoppingCartList.get(i).getId();
            shoppingCartMapper.removeById(id);
        }
        return 1;
    }

    //展示用户购物车商品
    @Override
    public List<ShoppingCart> ShowUserShoppingCart(Integer userid) {
        return shoppingCartMapper.showShoppingCartByUserid(userid);
    }

    //全选或全不选商品
    @Override
    public int SelectAll(Integer userid) {
        return shoppingCartMapper.selectAll(userid);
    }

    @Override
    public int NotSelectAll(Integer userid) {
        return shoppingCartMapper.notSelectAll(userid);
    }
    //改变单个商品状态
    @Override
    public int SelectOneOrNot(Integer id) {
        if (shoppingCartMapper.showSelectStatus(id)==1){
            return shoppingCartMapper.changeSelectedToZero(id);
        }
        else{
            return shoppingCartMapper.changeSelectedToOne(id);
        }
    }

    @Override
    public ShoppingCarList viewList(Integer userId) {
        User user = userMapper.selectByPrimaryKey(userId);
        ShoppingCarList result = new ShoppingCarList();
        result.setUser(user);
        List<ShoppingCart> actions = shoppingCartMapper.showShoppingCartByUserid(userId);
        List<ShoppingCart2> cars = new ArrayList<>();
        for(ShoppingCart action:actions){
            ShoppingCart2 car = new ShoppingCart2();
            Goods goods = goodsMapper.selectByPrimaryKey(action.getGoodid());
            car.setGoods(goods);
            cars.add(car);
        }
        result.setCars(cars);
        return result;
    }

    //购买已选择商品
    @Override
    public int BuySelectedGoods(Integer userid,String tradetime,String tradeplace) {
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.selectWhereSelectedEqualsOne(userid);
        for (int i = 0; i < shoppingCartList.size(); i++){
            Integer goodid= shoppingCartList.get(i).getGoodid();
            System.out.println(goodid);
            userService.buyTemp(userid,tradetime,tradeplace,goodid);
        }
        return 1;
    }

    @Override
    public int MoveToCollection(Integer userid) {
        List<ShoppingCart> shoppingCartList=shoppingCartMapper.selectWhereSelectedEqualsOne(userid);
        //System.out.println(shoppingCartList);
        for (int i=0;i<shoppingCartList.size();i++){
            int goodid=shoppingCartList.get(i).getGoodid();
            Goods good = goodsMapper.SelectGoodByGoodId(goodid);
            //System.out.println(good);
            Collection collection = new Collection();
            collection.setGoodid(good.getId());
            collection.setGoodname(good.getName());
            collection.setGoodprice(good.getPrice());
            collection.setUserid(userid);
            collection.setSelected(1);

            if (collectionMapper.userAndgoodExistOrNot(good.getId(),userid).isEmpty()){
                collectionMapper.insertToDatabase(collection);
            }
        }
        return 1;
    }
    //多选下单重置版
    @Override
    public int BuySelectedGoods2(Integer userid,String tradetime,String tradeplace) {
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.selectWhereSelectedEqualsOne(userid);
        String tradeNo = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
        for (int i = 0; i < shoppingCartList.size(); i++){
            int num = shoppingCartList.get(i).getNum();
            float price=shoppingCartList.get(i).getGoodprice()*num;
            Integer goodid= shoppingCartList.get(i).getGoodid();
            //System.out.println(goodid);
            userService.buyTemp2(userid,tradetime,tradeplace,goodid,price,tradeNo,num);
        }
        return 1;
    }

    @Override
    public int SetBuyNum(Integer id, Integer userid ,Integer num) {
        return shoppingCartMapper.setBuyNum(id,userid,num);

    }
}
